package com.yuandian.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.management.entity.ConfDataCenter;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.constant.Constants;
import com.yuandian.management.mapper.ConfDataCenterMapper;
import com.yuandian.management.service.ConfDataCenterService;
import com.yuandian.management.vo.DataCenterInfo;
import com.yuandian.management.vo.DataCenterParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数据中心表-Service实现
 * @author zfsn
 * @date 2023-7-31
 */
@Service
public class ConfDataCenterServiceImpl extends ServiceImpl<ConfDataCenterMapper, ConfDataCenter>
        implements ConfDataCenterService {

	@Autowired
	private ConfDataCenterMapper mapper;

	@Override
	public List<DataCenterInfo> queryDataCenterInfos(DataCenterParams params) {
		String name = params.getName();
		LambdaQueryWrapper<ConfDataCenter> wrapper = null;
		if (null != name) {
			wrapper = new LambdaQueryWrapper<ConfDataCenter>().like(ConfDataCenter::getName, params.getName());
		} else {
			wrapper = new LambdaQueryWrapper<>();
		}

		List<ConfDataCenter> confDataCenterList = this.list(wrapper);
		List<DataCenterInfo> dataList = new ArrayList<>();
		for (ConfDataCenter confDataCenter : confDataCenterList) {
			DataCenterInfo info = new DataCenterInfo();
			BeanUtils.copyProperties(confDataCenter, info);
			info.setCenterId(0);
			info.setTreeId(confDataCenter.getId());
			dataList.add(info);
		}

		List<DataCenterInfo> centerInfos = mapper.queryDataCenterAreas(params);
		dataList.addAll(centerInfos);
		return dataList;
	}

	@Override
	public DataCenterInfo queryDataCenterInfoById(Integer id) {
		ConfDataCenter dataCenter = this.getById(id);
		DataCenterInfo centerInfo = new DataCenterInfo();
		BeanUtils.copyProperties(dataCenter, centerInfo);
		centerInfo.setCenterId(0);
		centerInfo.setTreeId(dataCenter.getId());
		return centerInfo;
	}

	@Override
	public boolean updateDataCenterInfo(DataCenterInfo inParam) {
		ConfDataCenter confDataCenter = new ConfDataCenter();
		confDataCenter.setUpdateBy(SecurityUtils.getUser().getName());
		confDataCenter.setUpdateTime(LocalDateTime.now());
		BeanUtils.copyProperties(inParam, confDataCenter);

		return this.updateById(confDataCenter);
	}

	@Override
	public boolean checkDataExist(String name) {
		long num = this.count(new LambdaQueryWrapper<ConfDataCenter>().eq(ConfDataCenter::getName, name));

		return num > 0;
	}

	@Override
	public boolean insertDataCenterInfo(DataCenterInfo inParam) {
		ConfDataCenter dataCenter = new ConfDataCenter();
		dataCenter.setStatus(0);
		BeanUtils.copyProperties(inParam, dataCenter);
		String username = SecurityUtils.getUser().getName();
		dataCenter.setCreateBy(username);
		dataCenter.setCreateTime(LocalDateTime.now());
		dataCenter.setUpdateBy(username);
		dataCenter.setUpdateTime(LocalDateTime.now());

		return this.save(dataCenter);
	}

	@Override
	public boolean deleteDataByIds(String idStr) {
		List<Integer> ids = new ArrayList<>();
		if (idStr.contains(Constants.ENGLISH_COMMA)) {
			String[] idArr = idStr.split(Constants.ENGLISH_COMMA);
			ids = Stream.of(idArr).map(Integer::valueOf).collect(Collectors.toList());
		} else {
			ids.add(Integer.valueOf(idStr));
		}

		return this.removeByIds(ids);
	}

	@Override
	public boolean deleteDataByIds(Integer[] ids) {
		if (Objects.nonNull(ids)) {
			List<Integer> idList = Stream.of(ids).collect(Collectors.toList());
			return this.removeByIds(idList);
		}
		return false;
	}
}